package com.jl.basicframework.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.AndroidViewModel;

import com.alibaba.android.arouter.launcher.ARouter;
import com.jl.basicframework.viewModel.BaseViewModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/***
 * @author 作者:姜澜
 * 时间:2022/3/11
 * 邮箱:316879104@pp.com
 * 类简述:原则上所以fragment都需要继承他
 */
public abstract class BaseFragment <T extends ViewDataBinding, VM extends BaseViewModel> extends Fragment {

    protected View mRootView;
    /**
     * DataBinding
     */
    protected T dataBinding;

    /**
     * ViewModel
     */
    protected VM viewModel;


    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 重置视图
     */
    protected abstract void resetView();

    /**
     * 当前fragment是否加载
     */
    private boolean isLoad;

    /**
     * 刷新列表ui 用于activity通知fragment 刷新页面
     */
    public void refreshListUi(){
        //检查可见性 当fragment 不处于前台时拒绝调用刷新
        if (isLoad && isHidden()){
            updateList();
        }
    }

    /**
     * 列表数据更新
     */
    protected void updateList(){

    }

    /**
     * 返回xml布局文件id
     * @return 布局id
     */
    protected abstract int getLayoutId();

    /**
     * 返回viewModel
     * @return viewModel
     */
    protected abstract VM getViewModel();

    /**
     * 设置状态栏颜色
     */
    public void setStatusBarColor(int color){
        Window window = getActivity().getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(color);
    }

    /**
     * 设置状态栏图标颜色
     */
    public void setStatusBarIconColorBlack(){
        getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }


    /**
     * 设置土司
     */
    protected void showToast(String toastMessage){
        if (toastMessage==null || "".equals(toastMessage)){
            return;
        }
        Toast.makeText(getContext(),toastMessage,Toast.LENGTH_SHORT).show();
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        //绑定消息总线
        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }

        //阿里路由自动注入初始化
        ARouter.getInstance().inject(this);

        //设置viewModel
        viewModel = getViewModel();

        //初始化DataBing
        dataBinding = DataBindingUtil.inflate(inflater, getLayoutId(),container,false);
        mRootView = dataBinding.getRoot();

        //初始化数据
        initData();
        return mRootView;
    }

    @Override
    public void onResume() {
        super.onResume();

        if (!isLoad && !isHidden()){
            //当前fragment 标识为前台可见
            isLoad = true;
            //重置布局
            resetView();
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //标识为不可见
        isLoad = false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //注销消息总线
        EventBus.getDefault().unregister(this);
    }


    /**
     * 必须要有一个Subscribe，测试时报错
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String event) {

    }
}
